	var start = 0;
	var pageSize = 12;
	
	var selectState = 1;
	var selectDetailState = 1;
	var joinState = 0;
	
	var fultureId = "fultureId0";

	$(document).ready(function() 
	{
		getFultures();
		
		$("#getMoreFultures").click(function()
		{
			if(selectState == 1)
			{
				selectState = 0;
				
				$("#getMoreFultures").text("正在查询，请稍等"); 
				
				getFultures();
			}
		});
		
		$(".fultureDetilClick").live('click',function()
		{
			if(selectDetailState == 1)
			{
				selectDetailState = 0;
				
				$("#fultureDetail").animate({"top":$(document).scrollTop() + "px"});
				$("#fultureDetail").show(500);
				
				fultureId = $(this).attr("id");
				getFulture(fultureId.substr(9));
			}
		});
		
		$("#fultureDetailClose").click(function(){
			$("#fultureDetail").hide(500);
		});
		
		$("#fultureDetailJoin").click(function(){
			join();
		});
	});
	
	function getFultures()
	{
		jQuery.ajax({
		type: 'POST',        
		url: "/student/fultureFind.do",
		cache: false,
		data:"start=" + start + "&end=" + (start + pageSize - 1) + "&kind=time",
		dataType:"json",
		success: function(data){
			start += pageSize;
			
			var fultures = data.fultures;
			
			if(fultures.length == 13)
			{
				selectState = 1;
				$("#getMoreFultures").text("查看更多");
			}
			else
			{
				selectState = 2;
				$("#getMoreFultures").text("就这些啦~");
			}
			
			for(var i=1; i<fultures.length; i++)
			{
				var colum = "#colum" + (i-1) % 4;
				
				var inner = "<li><div id=\"fultureId" + fultures[i].id + "\" class=\"fultureDetilClick\"><a class=\"b_btn\"><span> <h4>" + fultures[i].date + "</h4><p>" + fultures[i].message + "</p></span> </a>";
				
				$(colum).append(inner);
			}
		},
		 error: function(XMLHttpRequest, textStatus, errorThrown) {
			 selectState = 1;
			 $("#getMoreFultures").text("查询失败，错误代码" + XMLHttpRequest.status);
			},
		});
	}
	
	function getFulture(id)
	{
		jQuery.ajax({
		type: 'POST',        
		url: "/student/fultureDetail.do",
		cache: false,
		data:"fultureId=" + id,
		dataType:"json",
		success: function(data){
			selectDetailState = 1;
			$("#fultureDetailDate").text(data.fulture.date);
			$("#fultureDetailMessage").text(data.fulture.message);
			
			var joinStudentid = data.joinedStudent;
			var joinedId = "";
			for(var i=1; i<joinStudentid.length; i++)
			{
				joinedId += joinStudentid[i].sid + ",";
			}
			$("#fultureDetailJoinedId").text(joinedId);

			if(data.fulture.state == 1000)
			{
				$("#fultureDetailJoin").text("我也要参与");
				
				joinState = 1;
			}
			else if(data.fulture.state == 1001)
			{
				$("#fultureDetailJoin").text("已参与");
				
				joinState = 2;
			}
			else if(data.fulture.state == 1002)
			{
				$("#fultureDetailJoin").text("（登陆后可参与）");
				
				joinState = 3;
			}
		},
		 error: function(XMLHttpRequest, textStatus, errorThrown) {
			 selectState = 1;
			 $("#imgViewDetailStudentId").text("查询失败，错误代码" + XMLHttpRequest.status);
			},
		});
	}
	
	function fultureJoin(id)
	{
		jQuery.ajax({
		type: 'POST',        
		url: "/student/fultureJoin.do",
		cache: false,
		data:"fultureId=" + id,
		dataType:"text",
		success: function(data){
			if(data.trim().localeCompare("success") == 0)
			{
				alert("成功参与~");
				getFulture(id);
			}
			else if(data.trim().localeCompare("unlogin") == -1)
			{
				window.location.href="/login.html";
			}
			else
			{
				alert("fail");
			}
		},
		 error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(XMLHttpRequest.status + "error");
			},
		});
	}
	
	function join()
	{
		if(joinState == 1)
		{
			fultureJoin(fultureId.substr(9));
		}
		else if(joinState == 2)
		{
			alert("您已经参与啦~");
		}
		else
		{
			window.location.href="/login.html";
		}
	}